<?php
	#-------------------------------------------------------------#
	# This file is part of the Kumomado - Virtual Window Platform #
	# for PHP. (c) 2009 Tsai, Min Che <tirme0812@gmail.com>       #
	# Please visit http://www.kumomado.org/ for more information. #
	#                                                             #
	# This software is licensed under the GNU License v3. Please  #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#

	/**
	 * @package libraries/xml/
	 * @author Tsai, Min Che
	 * @version 1.0
	 */
	class KumomadoXmlWriter extends XMLWriter {
		/**
		 * Constructor
		 * @param string $root_name A root element's name of a current xml document.
		 * @param string $xslt_path Path of a XSLT file.
		 */
		public function __construct($root_name, $xslt_path = null) {
			$this->openMemory();
			$this->setIndent(true);
			$this->setIndentString('	');
			$this->startDocument('1.0', 'UTF-8');
			if (isset($xslt_path) && file_exists($xslt_path)) {
				$this->writePi('xml-stylesheet', sprintf('type="text/xsl" href="%s"', $xslt_path));
			}
			$this->startElement($root_name);
		}
		/**
		 * Set an element with a text to a current xml document.
		 * @param string $name An element's name
		 * @param string $text An element's text
		 * @param array $attr An element's attributes
		 */
		public function setElement($name, $text, $attrs = null) {
			$this->startElement($name);
			if (is_array($attrs)) {
				foreach ($attrs as $attr => $value) {
					$this->setAttribute($attr, $value);
				}
			}
			if (is_bool($text)) {
				$text = $text ? 'true' : 'false';
			}
			$this->text($text);
			$this->endElement();
		}
		/**
		 * Set an attribute to element
		 * @param string $name An attribute's name
		 * @param mixed $value An attribute's value
		 */
		public function setAttribute($name, $value) {
			$this->startAttribute($name);
			$this->text($value);
			$this->endAttribute();
		}
		/**
		 * Construct elements and texts from an array.
		 * The array should contain an attribute's name in index part
		 * and a attribute's text in value part.
		 * @param array $array Contains attributes and texts
		 */
		public function fromArray($array){
			if (is_array($array)){
				foreach ($array as $key => $value){
					if (is_numeric($key)) {
						$key = 'Item';
					}
					if (is_array($value)){
						$this->startElement($key);
						$this->fromArray($value);
						$this->endElement();
					} else {
						$this->setElement($key, $value);
					}
				}
			}
		}
		/*public function fromAssocArray($array) {
			if (is_array($array)) {
				if (isset($array['tagName'])) {
					$this->startElement($array['tagName']);
					if (isset($array['attrs']) && is_array($array['attrs'])) {
						foreach ($array['attrs'] as $attr => $value) {
							$this->setAttribute($attr, $value);
						}
					}
					if (isset($array['childs']) && is_array($array['childs'])) {
						$this->fromAssocArray($array['childs']);
					} else {
						$this->text($array['text']);
					}
					$this->endElement();
				} else {
					$this->startElement('Item');
					//$this->fromAssocArray($array);
					$this->endElement();
				}
			}
		}*/
		/**
		 * Return the content of a current xml document.
		 * @return string Xml document
		 */
		public function getDocument(){
			$this->endElement();
			$this->endDocument();
			return $this->outputMemory();
		}
		/**
		 * Output the content of a current xml document.
		 */
		public function output(){
			header('Content-type: application/xml');
			echo $this->getDocument();
		}
	}
?>